<?php

namespace api\pay;

use api\pay\driver\Huifu;
use api\pay\driver\Tgpay;
use api\pay\driver\Wechat;
use api\pay\exception\PayException;

/**
 * Class Pay
 *
 * @package api\pay
 * @mixin Wechat
 */
class Pay
{
    private $driver;
    private $drivers;
    private $config = [];

    public function __construct($driver, $config = [])
    {
        if (!in_array($driver, ['wechat'])) {
            throw new PayException('驱动不存在');
        }
        $this->config = array_merge($this->config, $config);
        $this->setDriver($driver);
    }

    /**
     * 设置驱动
     *
     * @param string $driver 驱动名称
     * @return void
     */
    public function setDriver($driver)
    {
        if (!isset($this->drivers[$driver])) {
            $driver = ucfirst($driver);
            $class = '\\api\\pay\\driver\\' . $driver;
            $this->drivers[$driver] = new $class($this->config);
        }
        $this->driver = $this->drivers[$driver];
    }

    public function __call($name, $arguments)
    {
        return $this->driver->$name(...$arguments);
    }
}